/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dw2;

import java.io.IOException;
import java.io.File;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiUnavailableException;

/**
 *
 * @author Chad Bonivtch
 */
public class MusicClient {
    int i;
    private boolean isActive;
    File file;
    MidiFile midiFile = new MidiFile();
    MidiFile menuFile = new MidiFile();
    long position;
    protected double factor;

    public boolean isActive()
    {
        return isActive;
    }
    MusicClient()
    {
         midiFile.setFile("Music\\BackgroundMusic"+i+".mid");
         position = 0;
         isActive = false;
    }

    public void resetMusic() {
        midiFile.setFile("Music\\BackgroundMusic0.mid");
    }

    public void play() {
        isActive = true;
        midiFile.play(position,1);
    }

    public void playMenuMusic(int i) {
        try{menuFile.stop();}
        catch(Exception e) {}

        //Title music
        if( i == 0) {
            menuFile.setFile("Music\\TitleMusic.mid");
            menuFile.play(0,0);
        }
        //Death music
        if(i == 1) {
            menuFile.setFile("Music\\DeathMusic.mid");
            menuFile.play(0, 0);
        }
        //High Score menu music
        if(i == 2) {
            menuFile.setFile("Music\\HighScoreMusic.mid");
            menuFile.play(0, 1);
        }
        if(i == 3) {
            menuFile.setFile("Music\\HelpMusic.mid");
            menuFile.play(0, 0);
        }
    }

    public void playSpecialMusic() {
        midiFile.setFile("Music\\BossMusic.mid");
    }

//    public void stopSpecialMusic() {
//        midiFile.stop();
//        midiFile.setFile("Music\\BackgroundMusic"+i+".mid");
//        midiFile.play(position, 1);
//    }

    public void changeMusic() throws MidiUnavailableException, InvalidMidiDataException, IOException {
        i++;

        file = new File("Music\\BackgroundMusic"+i+".mid");

        if(file.exists()) {
            midiFile.setFile("Music\\BackgroundMusic"+i+".mid");
        }
        else {
            i = 0;
            midiFile.setFile("Music\\BackgroundMusic"+i+".mid");
        }
    }

    public void stopMusic() {
        isActive = false;
        midiFile.stop();
    }

    public void stopMenuMusic() {
        menuFile.stop();
    }

    public void pause() {
        position = midiFile.pause();
    }

    public void reset() {
        position = 0;
    }

    public void dangerTempo(double factor) {
        midiFile.setDanger(factor);
    }

    public void normalTempo() {
        midiFile.setNormal();
    }

    public double getTempo() {
        factor = midiFile.getTempo();
        return factor;
    }
}
